# 多阶段构建Dockerfile for Vue前端

# 第一阶段：构建阶段
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/satantime/puppeteer-node:18.20.8-bullseye-slim AS builder

WORKDIR /app

# 设置npm镜像源（可选，用于加速）
RUN npm config set registry https://registry.npmmirror.com

# 复制package文件并安装依赖（包含开发依赖用于构建）
COPY package*.json ./
RUN npm ci --silent

# 安装terser用于Vite构建压缩
RUN npm install terser --save-dev --silent

# 复制源代码并构建
COPY . .
RUN npm run build

# 第二阶段：Nginx服务阶段
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:1.25.5

# 设置时区 (Debian/Ubuntu系统使用apt)
RUN apt-get update && apt-get install -y tzdata curl && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo 'Asia/Shanghai' > /etc/timezone && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 创建nginx用户目录
RUN mkdir -p /var/cache/nginx/client_temp && \
    mkdir -p /var/cache/nginx/proxy_temp && \
    mkdir -p /var/cache/nginx/fastcgi_temp && \
    mkdir -p /var/cache/nginx/uwsgi_temp && \
    mkdir -p /var/cache/nginx/scgi_temp

# 从构建阶段复制静态文件
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制nginx配置
COPY nginx.conf /etc/nginx/nginx.conf

# 暴露端口
EXPOSE 80

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
  CMD curl -f http://localhost/ || exit 1

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]
